<?php
class V1_opus extends CI_Controller{
	 function V1_opus(){
	  	parent::__construct();
	  	$this->load->model('opus');
	 }
	 function table(){
	 	$htdocs=$this->load->get_var('htdocs');
	 	$data['tpl']=$tpl=$this->input->get('tpl',true);
	 	$clg=intval($this->input->get('clg'));
	 	$data['tpl']=$tpl=$this->input->get('tpl',true);
	 	$data['look']=$this->input->get('look',true);
	 	$data['cover']=$this->input->get('cover',true);
	 	$start=intval($this->input->get('srt'));
	 	$get['lth']=intval($this->input->get('lth'));$get['lth']=empty($get['lth'])?15:$get['lth'];
	 	$search='';
	 	if($this->input->get('search')){
	 		$data['search']=$search=$get['search']=$this->input->get('search',true);
	 	}
	 	if($this->input->server('REQUEST_METHOD')=='POST'){
	 		$sequence=$this->input->post('order');
	 		foreach($sequence as $key=>$value){
	 			$this->opus->update(array('where'=>array('id'=>intval($key)),'set'=>array('sequence'=>intval($value))));
	 		}
	 	}
	 	$this->load->model('resource');
		$T=$this->resource->opus($clg,$start,$get['lth'],$search);
		$data['opus']=$T['RE'];$data['total']=$T['TD'];
		$data['basic']='/opus/table.html?tpl='.$tpl.'&cover='.$data['cover'].'&clg='.$clg.'&hcs='.$htdocs['id'].'&'.http_build_query($get);
		$data['pagination']=$this->resource->pagination($data['basic'],$T['TD'],$get['lth']);
		$this->load->view('artist/opus.table.php',$data);
	 }
	 function write(){
	 	$htdocs=$this->load->get_var('htdocs');
	 	$data['tpl']=$tpl=$this->input->get('tpl',true);
	 	$clg=intval($this->input->get('clg'));
	 	$artist=intval($this->input->get('artist'));
	 	if($this->input->server('REQUEST_METHOD')=='POST'){
	 		$data['artist']['catalog']=$clg;
			$data['artist']['name']=$this->input->post('name');
			$data['artist']['photo']=$this->input->post('photo');
			$data['artist']['intro']=$this->input->post('intro');
			$data['artist']['enhancer']=intval($_SESSION['id']);
			$data['artist']['date']=time();
			$data['artist']['locked']=0;
			if(empty($data['artist']['name'])){
				$data['message']['name']='作家名称不可为空';
			}else{
				$data['artist']['id']=$this->artist->insert($data['artist']);
				header('Location: /artist/table.html?tpl='.$tpl.'&clg='.$clg.'&hcs='.$htdocs['id']);
		 		exit();
			}
	 	}
	 	if($artist!==0){
	 		$data['artist']=reset($this->artist->get(array('where'=>array('id'=>$artist,'locked'=>0))));
	 	}
	 	$this->load->view('artist/'.$tpl.'.php',$data);
	 }
}
?>